import cv2
import mediapipe as mp
import tkinter as tk
from PIL import Image, ImageTk
import controller
import pygame

def display():

    file = "./bgm/menu.mp3"
    pygame.mixer.init()
    track = pygame.mixer.music.load(file)
    mp_hands = mp.solutions.hands
    hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5)

    root = tk.Tk()
    root.title("节奏大师")
    root.geometry(f"{1280}x{720}+{50}+{50}")
    canvas = tk.Canvas(root, width=1280, height=720)
    canvas.pack()

    cap = cv2.VideoCapture(0)

    def destroy_window():
        cap.release()
        pygame.mixer.music.stop()
        root.destroy()
    play_photo = tk.PhotoImage(file="resource\\start.png")
    play_btn = tk.Button(root, image=play_photo,compound=tk.TOP, bg=root.cget('bg'), borderwidth=0)
    play_btn.place(x=0, y=0, width=100, height=50)

    rule_photo = tk.PhotoImage(file="resource\\rule.png")
    rule_btn = tk.Button(root, image=rule_photo,compound=tk.TOP, bg=root.cget('bg'), borderwidth=0)
    rule_btn.place(x=1180, y=0,width=100,height=50)

    quit_photo = tk.PhotoImage(file="resource\\quit.png")
    quit_btn = tk.Button(root, image=quit_photo,compound=tk.TOP, bg=root.cget('bg'), borderwidth=0)
    quit_btn.place(x=0, y=550,width=100,height=50)

    tip = (
        "提示：你可以通过移动右手食指来选择不同的功能。"
    )
    custom_font = ("Helvetica", 12, "bold")
    label = tk.Button(root, text=tip,font=custom_font)
    label.place(x=450,y=0)
    def update_frame():
        success, image = cap.read()
        if not success:
            print("Ignoring empty camera frame.")
            root.after(10, update_frame)
            return

        image = cv2.flip(image, 1)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image,(1280,720))
        results = hands.process(image)

        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                index_finger_x = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * \
                                 image.shape[1]
                index_finger_y = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * \
                                 image.shape[0]
                if play_btn.winfo_x() < index_finger_x < play_btn.winfo_x()+play_btn.winfo_width() and \
                        play_btn.winfo_y() < index_finger_y < play_btn.winfo_y()+play_btn.winfo_height():
                    destroy_window()
                    controller.turn_to_game()
                    return
                elif rule_btn.winfo_x() < index_finger_x < rule_btn.winfo_x() + rule_btn.winfo_width() and \
                        rule_btn.winfo_y() < index_finger_y < rule_btn.winfo_y() + rule_btn.winfo_height():
                    destroy_window()
                    controller.turn_to_rule()
                    return
                elif quit_btn.winfo_x() < index_finger_x < quit_btn.winfo_x() + quit_btn.winfo_width() and \
                        quit_btn.winfo_y() < index_finger_y < quit_btn.winfo_y() + quit_btn.winfo_height():
                    destroy_window()
                    controller.turn_to_quit()
                    return

        img = Image.fromarray(image)
        img = ImageTk.PhotoImage(image=img)
        canvas.create_image(0, 0, anchor=tk.NW, image=img)
        canvas.img = img
        root.after(10, update_frame)


    update_frame()
    pygame.mixer.music.play()
    root.mainloop()

if __name__ == '__main__':
    display()
